/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2022-11-11
 * Time:10:04
 */
class ListNode{
    int val;
    ListNode next;

    public ListNode(int val) {
        this.val = val;
    }
}
public class Test {
    public ListNode detectCycle(ListNode head) {
        if(head==null){
            return null;
        }
        ListNode fast=head;
        ListNode slow=head;

    }
    public ListNode removeElements(ListNode head, int val) {

        while(head!=null&&head.val==val){
            head=head.next;

        }
        if(head==null){
            return head;
        }
        ListNode cur=head;
        ListNode pre=cur;
        while(cur!=null){
            if(cur.val==val){
                pre.next=cur.next;
                cur=cur.next;
            }else{
                pre=cur;
                cur=cur.next;
            }
        }
        return head;
    }
    public ListNode FindKthToTail(ListNode head,int k) {
        if(k<0||head==null){
            return null;
        }
        ListNode fast=head;
        ListNode slow=head;
        for (int i = 0; i < k; i++) {
            fast=fast.next;
            if(fast==null){
                return null;
            }
        }
        while(fast!=null){
            fast=fast.next;
            slow=slow.next;
        }
        return slow;
    }
    public ListNode FindKthToTail1(ListNode head,int k) {
        if(k<=0|| head==null){
            return null;
        }
        ListNode fast=head;
        ListNode slow=head;
        // while(k-1!=0){
        //     fast=fast.next;
        //     if(fast==null){
        //         return null;
        //     }
        //     k--;
        // }
        for (int i = 0; i <k-1; i++) {
            fast=fast.next;
            if(fast==null){
                return null;
            }
        }
        while(fast.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        return slow;
//         if(head==null){
//             return head;
//         }
//         ListNode cur=head;
//         int size=0;
//         while (cur!=null){
//             cur=cur.next;
//             size++;
//         }
//         if(k>size){
//             return null;
//         }
//         cur=head;
//         for(int i=0;i<size-k;i++){
//             cur=cur.next;
//         }
//         return cur;
    }
    public ListNode middleNode(ListNode head) {
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        return slow;
    }
    public static ListNode reverseList(ListNode head) {
        if(head==null){
            return null;
        }
        ListNode cur=head;
        ListNode pre=head.next;

        head.next=null;
        while(pre!=null){
            ListNode curnext=pre.next;
            pre.next=cur;
            cur=pre;
            pre=curnext;
        }
        return cur;
    }
    //pre==cur
    //curnext=ret
    //cur=head
    public ListNode reverseList1(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }
        ListNode cur =head.next;
        head.next=null;
        //判断
        while (cur!=null){
            ListNode ret=cur.next;
            cur.next=head;
            head=cur;
            cur=ret;
        }
        //返回
        return head;
    }

    public static void main(String[] args) {
        ListNode listNode1=new ListNode(1);
        ListNode listNode2=new ListNode(2);
        ListNode listNode3=new ListNode(3);
        ListNode listNode4=new ListNode(4);
        ListNode listNode5=new ListNode(5);
        listNode1.next=listNode2;
        listNode2.next=listNode3;
        listNode3.next=listNode4;
        listNode4.next=listNode5;
        ListNode list=reverseList(listNode1);
        System.out.println();
    }
}
